package com.example.zyfypt516wh.apdater;

import android.content.Context;

import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.example.zyfypt516wh.R;
import com.example.zyfypt516wh.activities.ViewArticleActivity;
import com.example.zyfypt516wh.bean.ResBean;
import com.example.zyfypt516wh.common.Common;
import com.squareup.picasso.Picasso;

import java.util.List;

public class ArticleAdapter extends RecyclerView.Adapter {
    private List<ResBean> list;//向 rv 中填充的数据
    private Context context;//上下文
    private LayoutInflater layoutInflater;//动态布局
    //自定义 构造方法
    public ArticleAdapter(Context context)
    {
        this.context=context;
        layoutInflater=LayoutInflater.from(context);
    }
    //自定义 ViewHolder 子类，容纳 item 视图
    public class ViewHolder extends RecyclerView.ViewHolder {
        ImageView imageView;
        TextView tvtitle,tvauthor,tvtime;
        public ViewHolder(View itemView) {
            super(itemView);
            imageView=itemView.findViewById(R.id.imageView);
            tvtitle=itemView.findViewById(R.id.textView);
            tvauthor=itemView.findViewById(R.id.textView2);
            tvtime=itemView.findViewById(R.id.textView3);
        }
    }
    //自定义 设置数据 list
    public void setList(List<ResBean> list)
    {
        this.list=list; notifyDataSetChanged();//通知 RV 刷新数据
    }
    @NonNull
    @Override public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View v=layoutInflater.inflate(
                R.layout.item,parent,false); return new ViewHolder(v);
    }
    @Override public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, int position) {
        final ResBean bean=list.get(position);
        if(bean==null) return;
        ViewHolder viewHolder=(ViewHolder)holder;
        Picasso.get()//新版的 Picasso 方法改为 get()
                .load(Common.IMAGEURL+bean.getThumb())
                .placeholder(R.mipmap.ic_launcher)
                .into(viewHolder.imageView);
        viewHolder.tvtitle.setText(bean.getName());

        viewHolder.tvauthor.setText(bean.getAuthor());
        viewHolder.tvtime.setText(bean.getUpdate_Time());
//item 条目点击事件
        viewHolder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
//取出当前 item 的 id
                String id=bean.getId();

                Toast.makeText(context, ""+id, Toast.LENGTH_SHORT).show();
//Intent intent=new Intent(context, ViewArticleActivity.class);
//intent.putExtra("resid",bean.getId());
//intent.putExtra("userid",bean.getUserid());
//context.startActivity(intent);


                System.out.println("--文章点击--"+id);
                Toast.makeText(context, ""+id, Toast.LENGTH_SHORT).show();
                Intent intent=new Intent(context, ViewArticleActivity.class);
                intent.putExtra("resid",bean.getId());//传递资源 id

                intent.putExtra("userid",bean.getUserid());//传递用户 id
                context.startActivity(intent);
            }
        });
    }
    @Override public int getItemCount() {
        if(list!=null) return list.size(); else
            return 0;
    }





}